package com.ke.jdyd.http;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.io.IOException;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitFactory {
    public final static String BASEURLS_DOMAIN = "https://ke.4399tech.com/";
//  1、所有网络同时通过okhttp进行通讯

    private final Gson mGson;
    private OkHttpClient mOkHttpClient;
    private Retrofit mRetrofit;
    private int mTimeOut = 6; //超时时间 秒为单位
    public static RetrofitFactory mInstance;

    public static RetrofitFactory getInstance() {
        if (mInstance == null) {
            synchronized (RetrofitFactory.class) {
                if (mInstance == null) {
                    mInstance = new RetrofitFactory();
                }
            }
        }
        return mInstance;
    }

    public RetrofitFactory() {
        mGson = new GsonBuilder().setLenient().create();
        mOkHttpClient = new OkHttpClient().newBuilder()
                .readTimeout(mTimeOut, TimeUnit.SECONDS)//设置读超时
                .callTimeout(mTimeOut, TimeUnit.SECONDS)//请求超时
                .writeTimeout(mTimeOut, TimeUnit.SECONDS)//写超时
                .addInterceptor(new LogInterceptor())//拦截器 处理对应逻辑需求
                .retryOnConnectionFailure(true)//设置出现错误进行重新连接
                .build();
    }

    public <T> T getApi(Class<T> apiClass) {
        if (mRetrofit == null) {
            mRetrofit = new Retrofit.Builder()
                    .baseUrl(BASEURLS_DOMAIN)//都是我们当前应用的域名
                    .client(mOkHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())//用来转换json数据，将数据转换成对应的been
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//结合Rxjava
                    .build();
        }
        return mRetrofit.create(apiClass);
    }

    public static class LogInterceptor implements Interceptor {
        private String TAG = "okhttp";

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request()
                    .newBuilder()
                    //.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                    //                .addHeader("Accept-Encoding", "gzip, deflate")
                    //                .addHeader("Connection", "keep-alive")
                    //                .addHeader("Accept", "*/*")
                    //                .addHeader("Cookie", "add cookies here")
                    .build();

            Log.e(TAG, "request:" + request.toString());
            okhttp3.Response response = chain.proceed(chain.request());
            okhttp3.MediaType mediaType = MediaType.parse("application/json");
            String content = response.body().string();
            Log.e(TAG, "请求回来的参数 body:" + content);
            // 1、用来查看请求是否正确
            //2、返回值格式是否正确
            //查看请求数据 可通过fildder4 抓包查看数据
            return response.newBuilder()
                    .body(okhttp3.ResponseBody.create(mediaType, content))
                    //                .header("Authorization", Your.sToken)
                    .build();
            //return chain.proceed(request);
        }
    }
}
